package com.sky.sky_server.controller.admin;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sky.sky_common.result.Result;
import com.sky.sky_pojo.dto.CategoryDTO;
import com.sky.sky_pojo.entity.CategoryQueryPage;
import com.sky.sky_server.service.impl.CategoryServiceImpl;

import jakarta.validation.Valid;

@RestController("adminCategoryController")
@RequestMapping("/admin/category")
public class CategoryController {

    @Autowired
    private CategoryServiceImpl categoryServiceImpl;

    // 分页查询
    @GetMapping("/page")
    public Result page(@Valid CategoryQueryPage categoryQueryPage){
        return Result.success(categoryServiceImpl.page(categoryQueryPage));
    }

    // 启用，禁用分类
    @PostMapping("/status/{status}")
    public Result updateStatus(@PathVariable Integer status,@RequestParam Long id){
        int res = categoryServiceImpl.updateStatus(status, id);
        if(res > 0)
            return Result.success();
        return Result.error("操作失败");
    }

    // 新增分类
    @PostMapping
    public Result save(@RequestBody CategoryDTO categoryDTO){
        categoryServiceImpl.save(categoryDTO);
        return Result.success();
    }

    // 根据id删除分类
    @DeleteMapping
    public Result delete(@RequestParam Long id){
        int res = categoryServiceImpl.delete(id);
        if(res  == -1)
            return Result.error("分类下有关联的菜品，不能删除");
        return Result.success();
    }

    // 修改分类
    @PutMapping
    public Result updateCategorySort(@RequestBody CategoryDTO categoryDTO){
        categoryServiceImpl.updateCategorySort(categoryDTO);
        return Result.success();
    }


    // 根据类型查询分类
    @GetMapping("/list")
    public Result list(@RequestParam Integer type){
        // CategoryQueryPage categoryQueryPage = CategoryQueryPage.builder().type(type).page(1).pageSize(10).build();
        return Result.success(categoryServiceImpl.list(type));
    }
}
